home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 May / EnigmA AMIGA RUN 18 (1997)(G.R. Edizioni)(IT)[!][issue 1997-05][EAR-CD II].iso / earcd / dev / mui / mui37_de.lha / Amiga-E / Examples / Class3.e < prev    next >
Text File  |  1996-08-25  |  10KB  |  294 lines

  1. /*
  2. ** Demosource on how to use customclasses in E.
  3. ** Based on the C example 'Class3.c' by Stafan Stuntz.
  4. ** Translated TO E by Sven Steiniger
  5. **
  6. ** Sorry FOR some uppercase words in the comments. This IS because OF
  7. ** my AutoCase-dictionary
  8. */
  9.  
  10. OPT PREPROCESS
  11.  
  12. MODULE 'muimaster','libraries/mui','libraries/muip',
  13.        'intuition/classes','intuition/classusr','intuition/screens','intuition/intuition',
  14.        'utility/tagitem',
  15.        'amigalib/boopsi',
  16.        'mui/muicustomclass'
  17.  
  18. /***************************************************************************/
  19. /* Here is the beginning of our simple new class...                        */
  20. /***************************************************************************/
  21.  
  22. /*
  23. ** This is the instance data for our custom class.
  24. */
  25.  
  26. OBJECT mydata
  27.   x,y,sx,sy
  28. ENDOBJECT
  29.  
  30.  
  31. /*
  32. ** AskMinMax method will be called before the window is opened
  33. ** and before layout takes place. We need to tell MUI the
  34. ** minimum, maximum and default size of our object.
  35. */
  36.  
  37. PROC mAskMinMax(cl:PTR TO iclass,obj:PTR TO object,msg:PTR TO muip_askminmax)
  38.  
  39.   /*
  40.   ** let our superclass first fill in what it thinks about sizes.
  41.   ** this will e.g. add the size OF frame and inner spacing.
  42.   */
  43.  
  44.   doSuperMethodA(cl,obj,msg)
  45.  
  46.   /*
  47.   ** now add the values specific TO our object. note that we
  48.   ** indeed need TO *add* these values, not just set them!
  49.   */
  50.  
  51.   msg.minmaxinfo.minwidth := msg.minmaxinfo.minwidth + 100
  52.   msg.minmaxinfo.defwidth := msg.minmaxinfo.defwidth + 120
  53.   msg.minmaxinfo.maxwidth := msg.minmaxinfo.maxwidth + 500
  54.  
  55.   msg.minmaxinfo.minheight := msg.minmaxinfo.minheight + 40
  56.   msg.minmaxinfo.defheight := msg.minmaxinfo.defheight + 90
  57.   msg.minmaxinfo.maxheight := msg.minmaxinfo.maxheight + 300
  58.  
  59. ENDPROC 0
  60.  
  61.  
  62. /*
  63. ** Draw method is called whenever MUI feels we should render
  64. ** our object. This usually happens after layout is finished
  65. ** or when we need to refresh in a simplerefresh window.
  66. ** Note: You may only render within the rectangle
  67. **       _mleft(obj), _mtop(obj), _mwidth(obj), _mheight(obj).
  68. */
  69.  
  70. PROC mDraw(cl:PTR TO iclass,obj:PTR TO object,msg:PTR TO muip_draw)
  71. DEF data:PTR TO mydata
  72.  
  73.   data:=INST_DATA(cl,obj)
  74.  
  75.   /*
  76.   ** let our superclass draw itself first, area class would
  77.   ** e.g. draw the frame and clear the whole region. What
  78.   ** it does exactly depends on msg.flags.
  79.   **
  80.   ** Note: You *must* call the super method prior TO DO
  81.   ** anything ELSE, otherwise msg.flags will not be set
  82.   ** properly !!!
  83.   */
  84.  
  85.   doSuperMethodA(cl,obj,msg)
  86.  
  87.   /*
  88.   ** IF MADF_DRAWOBJECT isn't set, we shouldn't draw anything.
  89.   ** MUI just wanted TO update the frame OR something like that.
  90.   */
  91.  
  92.   IF (msg.flags AND MADF_DRAWUPDATE)  /* called from our input method */
  93.     IF data.sx OR data.sy
  94.       SetBPen(_rp(obj),_dri(obj).pens[SHINEPEN])
  95.       ScrollRaster(_rp(obj),data.sx,data.sy,_mleft(obj),_mtop(obj),_mright(obj),_mbottom(obj))
  96.       SetBPen(_rp(obj),0)
  97.       data.sx:=0
  98.       data.sy:=0
  99.     ELSE
  100.       SetAPen(_rp(obj),_dri(obj).pens[SHADOWPEN])
  101.       WritePixel(_rp(obj),data.x,data.y)
  102.     ENDIF
  103.   ELSEIF (msg.flags AND MADF_DRAWOBJECT)
  104.     SetAPen(_rp(obj),_dri(obj).pens[SHINEPEN])
  105.     RectFill(_rp(obj),_mleft(obj),_mtop(obj),_mright(obj),_mbottom(obj))
  106.   ENDIF
  107. ENDPROC 0
  108.  
  109.  
  110. PROC mSetup(cl:PTR TO iclass,obj:PTR TO object,msg:PTR TO muip_handleinput)
  111.  
  112.   IF doSuperMethodA(cl,obj,msg)=NIL THEN RETURN FALSE
  113.   Mui_RequestIDCMP(obj,IDCMP_MOUSEBUTTONS OR IDCMP_RAWKEY)
  114.  
  115. ENDPROC MUI_TRUE
  116.  
  117.  
  118. PROC mCleanup(cl:PTR TO iclass,obj:PTR TO object,msg:PTR TO muip_handleinput)
  119.  
  120.   Mui_RejectIDCMP(obj,IDCMP_MOUSEBUTTONS OR IDCMP_RAWKEY)
  121.  
  122. ENDPROC doSuperMethodA(cl,obj,msg)
  123.  
  124.  
  125. /* in mSetup() we said that we want get a message IF mousebuttons OR keys pressed
  126. ** so we have TO define the input-handler
  127. ** Note : this IS really a good example, because it shows how TO use critical events
  128. **        carefully:
  129. **        IDCMP_MOUSEMOVE IS only needed when left-mousebutton IS pressed, so
  130. **        we dont request this UNTIL we get a SELECTDOWN-message and we reject
  131. **        IDCMP_MOUSEMOVE immeditly after we get a SELECTUP-message
  132. */
  133.  
  134. PROC mHandleInput(cl:PTR TO iclass,obj:PTR TO object,msg:PTR TO muip_handleinput)
  135. #define _between(a,x,b) (((x)>=(a)) AND ((x)<=(b)))
  136. #define _isinobject(x,y) (_between(_mleft(obj),(x),_mright(obj)) AND _between(_mtop(obj),(y),_bottom(obj)))
  137.  
  138. DEF data:PTR TO mydata,
  139.     selectdummy
  140.  
  141.   data:=INST_DATA(cl,obj)
  142.  
  143.   IF selectdummy:=msg.muikey
  144.     SELECT selectdummy
  145.       CASE MUIKEY_LEFT   ; data.sx:=-1; Mui_Redraw(obj,MADF_DRAWUPDATE)
  146.       CASE MUIKEY_RIGHT  ; data.sx:= 1; Mui_Redraw(obj,MADF_DRAWUPDATE)
  147.       CASE MUIKEY_UP     ; data.sy:=-1; Mui_Redraw(obj,MADF_DRAWUPDATE)
  148.       CASE MUIKEY_DOWN   ; data.sy:= 1; Mui_Redraw(obj,MADF_DRAWUPDATE)
  149.     ENDSELECT
  150.   ENDIF
  151.  
  152.   IF msg.imsg
  153.     selectdummy:=msg.imsg.class
  154.     SELECT selectdummy
  155.       CASE IDCMP_MOUSEBUTTONS
  156.         IF msg.imsg.code=SELECTDOWN
  157.           IF _isinobject(msg.imsg.mousex,msg.imsg.mousey)
  158.             data.x:=msg.imsg.mousex
  159.             data.y:=msg.imsg.mousey
  160.             Mui_Redraw(obj,MADF_DRAWUPDATE)
  161.  
  162.             -> only request IDCMP_MOUSEMOVE IF we realy need it
  163.             Mui_RequestIDCMP(obj,IDCMP_MOUSEMOVE)
  164.           ENDIF
  165.         ELSE
  166.  
  167.           -> reject IDCMP_MOUSEMOVE because THEN lmb IS no longer pressed
  168.           Mui_RejectIDCMP(obj,IDCMP_MOUSEMOVE)
  169.         ENDIF
  170.       CASE IDCMP_MOUSEMOVE
  171.         IF _isinobject(msg.imsg.mousex,msg.imsg.mousey)
  172.           data.x:=msg.imsg.mousex
  173.           data.y:=msg.imsg.mousey
  174.           Mui_Redraw(obj,MADF_DRAWUPDATE)
  175.         ENDIF
  176.     ENDSELECT
  177.   ENDIF
  178.  
  179. ENDPROC doSuperMethodA(cl,obj,msg)
  180.  
  181. /*
  182. ** Here comes the dispatcher FOR our custom class.
  183. ** Unknown/unused methods are passed to the superclass immediately.
  184. */
  185.  
  186. PROC myDispatcher(cl:PTR TO iclass,obj:PTR TO object,msg:PTR TO msg)
  187. DEF methodID
  188.  
  189.   methodID:=msg.methodid
  190.   SELECT methodID
  191.     CASE MUIM_AskMinMax    ;  RETURN mAskMinMax  (cl,obj,msg)
  192.     CASE MUIM_Draw         ;  RETURN mDraw       (cl,obj,msg)
  193.     CASE MUIM_HandleInput  ;  RETURN mHandleInput(cl,obj,msg)
  194.     CASE MUIM_Setup        ;  RETURN mSetup      (cl,obj,msg)
  195.     CASE MUIM_Cleanup      ;  RETURN mCleanup    (cl,obj,msg)
  196.   ENDSELECT
  197.  
  198. ENDPROC doSuperMethodA(cl,obj,msg)
  199.  
  200.  
  201.  
  202. /***************************************************************************/
  203. /* Thats all there is about it. Now lets see how things are used...        */
  204. /***************************************************************************/
  205.  
  206. PROC main() HANDLE
  207. DEF app=NIL,window,myobj,
  208.     mcc=NIL:PTR TO mui_customclass,
  209.     sigs=0
  210.  
  211.   IF (muimasterbase:=OpenLibrary(MUIMASTER_NAME, MUIMASTER_VMIN))=NIL THEN
  212.      Raise('Failed TO open muimaster.library')
  213.  
  214.  
  215.   /* Create the NEW custom class with a call TO MUI_CreateCustomClass(). */
  216.   /* Caution: This function returns not a struct IClass, BUT a           */
  217.   /* struct MUI_CustomClass which contains a struct IClass TO be         */
  218.   /* used with NewObject() calls.                                        */
  219.   /* Note well: MUI creates the dispatcher hook FOR you, you may         */
  220.   /* *not* use its h_Data field! IF you need custom data, use the        */
  221.   /* cl_UserData OF the IClass structure!                                */
  222.  
  223.   /* E-note: Create the NEW custom class with a call TO eMui_CreateCustomClass().*/
  224.   /* Big thanks TO Jan Hendrik Schulz FOR creating eMui_CreateCustomClass()      */
  225.  
  226.   IF (mcc:=eMui_CreateCustomClass(NIL,MUIC_Area,NIL,SIZEOF mydata,{myDispatcher}))=NIL THEN
  227.       Raise('Could not create custom class.')
  228.  
  229.   app:=ApplicationObject,
  230.       MUIA_Application_Title      , 'Class3',
  231.       MUIA_Application_Version    , '$VER: Class3 12.9 (21.11.95)',
  232.       MUIA_Application_Copyright  , '©1995, Stefan Stuntz',
  233.       MUIA_Application_Author     , 'Stefan Stuntz',
  234.       MUIA_Application_Description, 'Demonstrate the use OF custom classes.',
  235.       MUIA_Application_Base       , 'CLASS3',
  236.  
  237.       SubWindow, window:=WindowObject,
  238.           MUIA_Window_Title, 'A rather complex custom class',
  239.           MUIA_Window_ID   , "CLS3",
  240.           WindowContents, VGroup,
  241.  
  242.               Child, TextObject,
  243.                   TextFrame,
  244.                   MUIA_Background, MUII_TextBack,
  245.                   -> E-note : center this text means inserting a <ESC c> which IS usually \ec
  246.                   MUIA_Text_Contents, '\ecPaint with mouse,\nscroll with cursor keys.',
  247.                  End,
  248.  
  249.               Child, myobj:=NewObjectA(mcc.mcc_class,NIL,
  250.                                 [TextFrame,
  251.                                  TAG_DONE,0]),
  252.  
  253.  
  254.           End,
  255.  
  256.       End,
  257.   End
  258.  
  259.   IF app=NIL THEN Raise('Failed TO create Application')
  260.  
  261.   set(window,MUIA_Window_DefaultObject,myobj)
  262.  
  263.   doMethodA(window,[MUIM_Notify,MUIA_Window_CloseRequest,MUI_TRUE,
  264.             app,2,MUIM_Application_ReturnID,MUIV_Application_ReturnID_Quit])
  265.  
  266.  
  267. /*
  268. ** This is the ideal input loop for an object oriented MUI application.
  269. ** Everything is encapsulated in classes, no return ids need to be used,
  270. ** we just check if the program shall terminate.
  271. ** Note that MUIM_Application_NewInput expects sigs to contain the result
  272. ** from Wait() (or 0). This makes the input loop significantly faster.
  273. */
  274.  
  275.     set(window,MUIA_Window_Open,MUI_TRUE)
  276.  
  277.     WHILE Not(doMethodA(app,[MUIM_Application_NewInput,{sigs}]) = MUIV_Application_ReturnID_Quit)
  278.       IF sigs THEN sigs := Wait(sigs)
  279.     ENDWHILE
  280.  
  281.     set(window,MUIA_Window_Open,FALSE)
  282.  
  283. /*
  284. ** Shut down...
  285. */
  286.  
  287. EXCEPT DO
  288.     IF app THEN Mui_DisposeObject(app)                /* dispose all objects. */
  289.     IF mcc THEN Mui_DeleteCustomClass(mcc)            /* delete the custom class. */
  290.     IF muimasterbase THEN CloseLibrary(muimasterbase) /* close library */
  291.     IF exception THEN WriteF('\s\n',exception)
  292. ENDPROC
  293.  
  294.